package com.fucai.utils;

import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @description: List转换器
 * @author: zhuchi
 * @create: 2023-03-23 09:29
 * @Version 1.0
 **/
public class ListConverterUtil<T> {

    /**
     * 将List数据转换为List<T>
     * @param source 资源数据
     * @param target 转换对象类型
     * @return
     */
    public static <T> List<T> converterList(List source, Class<T> target) {
        if (!CollectionUtils.isEmpty(source)) {
            List<T> results = new ArrayList<>();
            try {
                Iterator iterator = source.iterator();
                while (iterator.hasNext()) {
                    T t = target.newInstance();
                    BeanUtils.copyProperties(iterator.next(), t);
                    results.add(t);
                }
            } catch (Exception var6) {
                var6.printStackTrace();
            }

            return results;
        } else {
            return new ArrayList<>();
        }
    }

}
